﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using AdventureWorks.Entities;
using AdventureWorksData.DAO;

namespace AdventureWorksTests.Fakes
{
	public class FakeGenericRepository<T> : IGenericRepository<T> where T : Entity
	{
		private readonly FakeDbSet<T> _db;

		public FakeGenericRepository()
		{
			_db = new FakeDbSet<T>();
		}

		
		public void Delete(int id)
		{
			T entity = _db.Find(id);
			Delete(entity);
		}

		public void Delete(T entityToDelete)
		{
			_db.Remove(entityToDelete);
		}

		public IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "")
		{
			var query = _db.AsQueryable();
			if (filter != null)
			{
				query = query.Where(filter);
			}

			foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
			{
				query = query.Include(includeProperty);
			}

			return orderBy != null ? orderBy(query).ToList() : query.ToList();
		}

		public T Get(int id)
		{
			return _db.Find(id);
		}

		public void Insert(T entity)
		{
			_db.Add(entity);
		}

		public void Update(T entityToUpdate)
		{
			_db.Update(entityToUpdate);
		}

		public void Dispose()
		{
			throw new NotImplementedException();
		}
	}
}
